package com.yyh.controller;


import java.util.ArrayList;
import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import com.yyh.entity.Roles;
import com.yyh.entity.Userroles;
import com.yyh.service.IUserrolesService;
import com.yyh.util.Result;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author yangyueheng
 * @since 2020-06-02
 */
@RestController
@RequestMapping("/userroles")
public class UserrolesController {
	@Autowired
	IUserrolesService service;
	@PostMapping("/getRolesByUsersId")
	public Object getRolesByUsersId(@RequestParam Integer uid) {
		List<Roles> List = service.getRolesByUsersId(uid);
		return List;
		
	}

	@PostMapping("/addRolesByUsersId")
	public Object addRolesByUsersId(@RequestParam Integer uid,@RequestParam String rids) {
	List<Userroles> list = new ArrayList<Userroles>();
		String[] arr = rids.split(",");
		for(String s:arr) {
		Integer rid = Integer.valueOf(s);
		Userroles ur = new Userroles(0,uid,rid);
		list.add(ur);
	}
		boolean res = service.saveBatch(list);
		if(res) {
			return new Result(0,"添加成功");
		}else {
			return new Result(1,"添加失败");
		}		
	}

	@PostMapping("/removeRolesByUserId")
	public Object removeRolesByUserId(@RequestParam Integer uid,@RequestParam String rids) {
		boolean res = service.removeRolesByUserId(uid,rids);
		if(res) {
			return new Result(0,"批量移除成功");
		}else {
			return new Result(1,"批量移除失败");
		}
	}
}